# -*- coding:UTF-8 -*-
'''
Created on 2013-6-6

@author: Administrator
'''
# 新式类和经典类的最大不同在于，所有新式类必须继承至少一个父类

# 经典
'''
1.方法仅仅是类内部定义的函数
2.方法只有在其所属的类拥有实例时，才能被调用
3.任何方法定义中的第一个参数都是变量self，表示调用者自己
变量前加__将属性私有化，会混淆为self._className__attr
在属性或方法前加_可以防止用from module import 来导入变量
545
'''
class MyNewObjectType:
    ''''''
    pass

class AddrBookentry(object):
    'address book entry class'
    def __init__(self, nm, ph):
        self.name = nm
        self.phone = ph
        print 'Created instance for: ', self.name
    
    def updatePhone(self, newph):
        self.phone = newph
        print 'Updated phone# for:', self.name
        
class EmplAddrBookentry(AddrBookentry):
    'Employee Address Book Entry class'
    def __init__(self, nm, ph, em):
        AddrBookentry.__init__(self, nm, ph)
        self.empid = id
        self.email = em
    
    
john = AddrBookentry('John Doe', '408-555-1212')
jane = AddrBookentry('Jane Doe', '650-555-1212')

print john
print john.name
print jane.name

# 类的数据属性
class C(object):
    foo = 100
    
print C.foo
print C.__name__

# 可以使用装饰来替代
class TestStaticMethod:
    def foo():
        print 'calling static method foo()'
    foo = staticmethod(foo)
class TestClassMethod:
    def foo(cls):
        print 'calling class method foo()'
        print 'foo() is part of class: ', cls.__name__
    foo = classmethod(foo)

tsm = TestStaticMethod()
TestStaticMethod.foo()
tsm.foo()

tcm = TestClassMethod()
TestClassMethod.foo()
tcm.foo()

class P(object):
    def foo(self):
        print 'Hi, I am P-foo()'
        
p = P()
p.foo()

class C1(P):
    def foo(self):
        # 上面这两种方式都可以调用
        # P.foo(self)
        # super(C, self).foo()
        print 'Hi, I am C-foo()'
c1 = C1()
c1.foo()
P.foo(c1)
# 任意项迭代器
class AnyIter(object):
    def __init__(self, data, safe=False):
        self.safe = safe
        self.iter = iter(data)
        
    def __iter__(self):
        return self
        
    def next(self, howmany=1):
        retval = []
        for eachItem in range(howmany):
            try:
                retval.append(self.iter.next())
            except StopIteration:
                if self.safe:
                    break
                else:
                    raise
        return retval
a = AnyIter(range(10))
i = iter(a)
for j in range(1, 5):
    print j, ':', i.next(j)
